package com.bocai.grainshop.order.listener;

import com.bocai.common.to.mq.SeckillOrderTo;
import com.bocai.grainshop.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "order.seckill.order.queue")
@Slf4j
public class SeckillOrderCreateListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void CreateSeckillOrder(Message message, Channel channel, SeckillOrderTo to) throws IOException {
        log.info("收到秒杀单信息--------->订单号:{}"+to.getOrderSn());
        try {
            orderService.createSeckillOrder(to);
            //TODO 订单创建成功 签收消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception E){
            //TODO 发送异常拒签
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
